package Year21_Month10.day1025;

public class LinkedList<E> {

    //先写结构
    public static class ListNode<E>{
        E elem;//内容
        ListNode<E> prev;//指向上一个节点
        ListNode<E> next;//指向下一个节点

        public ListNode(E e){
            elem = e;
        }
    }
    //首先写成员变量
    E elem;
    ListNode<E> first;//头结点
    ListNode<E> last;//尾结点
    int size;

    //头插
    public void addFirst(E e){
        ListNode newNode = new ListNode<>(e);
        if(first == null){
            last = newNode;
        }else{
            first.prev = newNode;
            newNode.next = first;
        }
        first = newNode;
        size++;
    }

    //尾插
    public void addLast(E e){
        ListNode newNode = new ListNode<>(e);
        if(last == null){
            first = newNode;
        }else{
            last.next = newNode;
            newNode.prev = last;
        }
        last = newNode;
        size++;
    }

    //任意位置插入
    public void addIndex(int index,E e){
        //首先检测参数
        if(index < 0 || index > size){
            throw new IndexOutOfBoundsException("下标越界了~~~~");
        }
        //现在准备插入
        if(index == 0){
            addFirst(e);
            return;
        }
        if(index == size){
            addLast(e);
        }else{
            ListNode code = new ListNode(e);
            int count = 0;//计数器
            ListNode key = first;
            while(count != index){
                key = key.next;
                count++;
            }
            key.prev.next = code;
            code.prev = key.prev;
            code.next = key;
            key.prev = code;
        }
        size++;
    }

    //头删
    public void removeFirst(){
        //处理空节点
        if(null == first){
            return;
        }
        //处理只有一个节点的问题
        if(first == last){
            first = null;
            last = null;
        }else {
            //这里最少有两个节点
            first = first.next;
            first.prev = null;
        }
        size--;
    }

    //尾删
    public void removeLast(){
        if(last == null){
            return;
        }
        if(last == first){
            first = null;
            last = null;
        }else{
            last = last.prev;
            last.next = null;
        }
        size--;
    }

    //寻找某一对象的下标
    public int indexOf(E e){
        ListNode cur = first;
        int count = 0;
        while(cur != null){
            if(cur.elem.equals(e)){
                return count;
            }
            count++;
            cur = cur.next;
        }
        return -1;
    }

    //寻找某一对象是否包含在链表当中
    public boolean contains(E e){
        return indexOf(e) != -1;
    }

    //删除值为e的元素
    public boolean remove(E e){
        if(first == null){
            return false;
        }
        if(first == last){
            first = null;
            last = null;
            return  true;
        }
        ListNode found = first;
        while(found != null){
            if(found.elem == e){
                found.prev.next = found.next;
                found.next.prev = found.prev;
                return true;
            }
        }
        return false;
    }

    //求长度
    public int size(){
        return size;
    }
    public String toString(){
        String s = "[";
        if(first == null){
            s += "]";
            return s;
        }
        ListNode cur = first;
        while(cur.next != null){
            s += cur.elem + ",";
            cur = cur.next;
        }
        s += cur.elem + "]";
        return s;
    }

    public static void main(String[] args) {
        LinkedList<Integer> list  = new LinkedList<>();
        list.addLast(1);
        list.addLast(2);
        list.addLast(3);
        list.addLast(4);
        list.addFirst(0);
        System.out.println(list);

        list.removeFirst();
        System.out.println(list);
        list.removeLast();
        System.out.println(list);
        list.addIndex(0,0);
        System.out.println(list);
        System.out.println(list.contains((Integer) 1));
    }
}
